<template>
  <div class="rizhi">
    <el-table v-loading="loading" :data="tableData" style="width: 100%">
      <el-table-column fixed type="index" label="日志行" width="80" />
      <el-table-column prop="type" label="日志类型" width="120" />
      <el-table-column prop="name" label="日志说明" />
      <el-table-column label="请求参数">
        <template slot-scope="scope">
          <el-popover v-if="scope.row.requestData" placement="top-start" title="" width="500" trigger="hover" :content="scope.row.requestData">
            <el-button slot="reference" @click="copyToClip(JSON.stringify(scope.row.requestData))">复制</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="响应结果">
        <template slot-scope="scope">
          {{ scope.row.responseData }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="写入时间" width="220">
        <template slot-scope="scope">
          <i class="el-icon-time" /><span>{{ scope.row.createTime| timeFilter }}</span>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="form.page"
      :page-size="form.pageSize"
      :total="total"
      :page-sizes="[5, 20, 50]"
      layout="total, sizes, prev, pager, next, jumper"
      @current-change="handlePageChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
import { allRizhi } from '@/api/renwu'
import { parseTime } from '@/utils'

export default {
  name: 'AllRizhi',
  filters: {
    timeFilter(time) {
      return parseTime(time)
    }
  },
  data() {
    return {
      tableData: [],
      form: {
        page: 1,
        limit: 5
      },
      total: 0,
      loading: true
    }
  },
  created() {
    this.getList()
  },
  methods: {
    copyToClip(content) {
      const aux = document.createElement('input')
      aux.setAttribute('value', content)
      document.body.appendChild(aux)
      aux.select()
      document.execCommand('copy')
      document.body.removeChild(aux)
      if (content) {
        this.$message.success('复制成功')
      } else {
        this.$message.warning('无数据')
      }
    },
    async getList() {
      this.loading = true
      const { result, total } = await allRizhi(this.form)
      this.tableData = result
      this.total = total
      this.loading = false
    },
    handlePageChange(page) {
      this.form.page = page
      this.getList()
    },
    handleSizeChange(size) {
      this.form.page = 1
      this.form.limit = size
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.rizhi {
  padding: 20px;

}
</style>
